<!DOCTYPE html>
<html lang="" xml:lang="">
<head>

  <meta charset="utf-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <title>第 3 章 R基础 | 生态统计学原理</title>
  <meta name="description" content="这是生态学统计基础的课程文件" />
  <meta name="generator" content="bookdown 0.40 and GitBook 2.6.7" />

  <meta property="og:title" content="第 3 章 R基础 | 生态统计学原理" />
  <meta property="og:type" content="book" />
  
  <meta property="og:description" content="这是生态学统计基础的课程文件" />
  <meta name="github-repo" content="rstudio/bookdown-demo" />

  <meta name="twitter:card" content="summary" />
  <meta name="twitter:title" content="第 3 章 R基础 | 生态统计学原理" />
  
  <meta name="twitter:description" content="这是生态学统计基础的课程文件" />
  

<meta name="author" content="沈国春、李勤，华东师范大学生态与环境科学学院" />



  <meta name="viewport" content="width=device-width, initial-scale=1" />
  <meta name="apple-mobile-web-app-capable" content="yes" />
  <meta name="apple-mobile-web-app-status-bar-style" content="black" />
  
  
<link rel="prev" href="如何开始.html"/>

<script src="libs/jquery-3.6.0/jquery-3.6.0.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/fuse.js@6.4.6/dist/fuse.min.js"></script>
<link href="libs/gitbook-2.6.7/css/style.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-table.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-bookdown.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-highlight.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-search.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-fontsettings.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-clipboard.css" rel="stylesheet" />








<link href="libs/anchor-sections-1.1.0/anchor-sections.css" rel="stylesheet" />
<link href="libs/anchor-sections-1.1.0/anchor-sections-hash.css" rel="stylesheet" />
<script src="libs/anchor-sections-1.1.0/anchor-sections.js"></script>


<style type="text/css">
pre > code.sourceCode { white-space: pre; position: relative; }
pre > code.sourceCode > span { line-height: 1.25; }
pre > code.sourceCode > span:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode > span { color: inherit; text-decoration: inherit; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
pre > code.sourceCode { white-space: pre-wrap; }
pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
}
pre.numberSource code
  { counter-reset: source-line 0; }
pre.numberSource code > span
  { position: relative; left: -4em; counter-increment: source-line; }
pre.numberSource code > span > a:first-child::before
  { content: counter(source-line);
    position: relative; left: -1em; text-align: right; vertical-align: baseline;
    border: none; display: inline-block;
    -webkit-touch-callout: none; -webkit-user-select: none;
    -khtml-user-select: none; -moz-user-select: none;
    -ms-user-select: none; user-select: none;
    padding: 0 4px; width: 4em;
    color: #aaaaaa;
  }
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa;  padding-left: 4px; }
div.sourceCode
  {   }
@media screen {
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
}
code span.al { color: #ff0000; font-weight: bold; } /* Alert */
code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
code span.at { color: #7d9029; } /* Attribute */
code span.bn { color: #40a070; } /* BaseN */
code span.bu { color: #008000; } /* BuiltIn */
code span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
code span.ch { color: #4070a0; } /* Char */
code span.cn { color: #880000; } /* Constant */
code span.co { color: #60a0b0; font-style: italic; } /* Comment */
code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
code span.do { color: #ba2121; font-style: italic; } /* Documentation */
code span.dt { color: #902000; } /* DataType */
code span.dv { color: #40a070; } /* DecVal */
code span.er { color: #ff0000; font-weight: bold; } /* Error */
code span.ex { } /* Extension */
code span.fl { color: #40a070; } /* Float */
code span.fu { color: #06287e; } /* Function */
code span.im { color: #008000; font-weight: bold; } /* Import */
code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
code span.kw { color: #007020; font-weight: bold; } /* Keyword */
code span.op { color: #666666; } /* Operator */
code span.ot { color: #007020; } /* Other */
code span.pp { color: #bc7a00; } /* Preprocessor */
code span.sc { color: #4070a0; } /* SpecialChar */
code span.ss { color: #bb6688; } /* SpecialString */
code span.st { color: #4070a0; } /* String */
code span.va { color: #19177c; } /* Variable */
code span.vs { color: #4070a0; } /* VerbatimString */
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
</style>

<style type="text/css">
  
  div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
</style>

<link rel="stylesheet" href="style.css" type="text/css" />
</head>

<body>



  <div class="book without-animation with-summary font-size-2 font-family-1" data-basepath=".">

    <div class="book-summary">
      <nav role="navigation">

<ul class="summary">
<li><a href="./">生态统计学原理</a></li>

<li class="divider"></li>
<li class="chapter" data-level="1" data-path="index.html"><a href="index.html"><i class="fa fa-check"></i><b>1</b> 前言</a>
<ul>
<li class="chapter" data-level="1.1" data-path="index.html"><a href="index.html#为什么要学生态统计"><i class="fa fa-check"></i><b>1.1</b> 为什么要学生态统计</a></li>
<li class="chapter" data-level="1.2" data-path="index.html"><a href="index.html#为什么要学r语言"><i class="fa fa-check"></i><b>1.2</b> 为什么要学R语言</a></li>
<li class="chapter" data-level="1.3" data-path="index.html"><a href="index.html#如何使用和修改本书"><i class="fa fa-check"></i><b>1.3</b> 如何使用和修改本书</a></li>
<li class="chapter" data-level="1.4" data-path="index.html"><a href="index.html#课程助教"><i class="fa fa-check"></i><b>1.4</b> 课程助教</a></li>
</ul></li>
<li class="chapter" data-level="2" data-path="如何开始.html"><a href="如何开始.html"><i class="fa fa-check"></i><b>2</b> 如何开始</a>
<ul>
<li class="chapter" data-level="2.1" data-path="如何开始.html"><a href="如何开始.html#计算机的一些基本原理"><i class="fa fa-check"></i><b>2.1</b> 计算机的一些基本原理</a></li>
<li class="chapter" data-level="2.2" data-path="如何开始.html"><a href="如何开始.html#安装r程序"><i class="fa fa-check"></i><b>2.2</b> 安装R程序</a></li>
<li class="chapter" data-level="2.3" data-path="如何开始.html"><a href="如何开始.html#运行r程序"><i class="fa fa-check"></i><b>2.3</b> 运行R程序</a></li>
<li class="chapter" data-level="2.4" data-path="如何开始.html"><a href="如何开始.html#停止r程序"><i class="fa fa-check"></i><b>2.4</b> 停止R程序</a></li>
<li class="chapter" data-level="2.5" data-path="如何开始.html"><a href="如何开始.html#安装rstudio"><i class="fa fa-check"></i><b>2.5</b> 安装Rstudio</a></li>
<li class="chapter" data-level="2.6" data-path="如何开始.html"><a href="如何开始.html#获取帮助"><i class="fa fa-check"></i><b>2.6</b> 获取帮助</a>
<ul>
<li class="chapter" data-level="2.6.1" data-path="如何开始.html"><a href="如何开始.html#本地帮助文档"><i class="fa fa-check"></i><b>2.6.1</b> 本地帮助文档</a></li>
<li class="chapter" data-level="2.6.2" data-path="如何开始.html"><a href="如何开始.html#在线帮助"><i class="fa fa-check"></i><b>2.6.2</b> 在线帮助</a></li>
<li class="chapter" data-level="2.6.3" data-path="如何开始.html"><a href="如何开始.html#直接询问ai"><i class="fa fa-check"></i><b>2.6.3</b> 直接询问AI</a></li>
</ul></li>
<li class="chapter" data-level="2.7" data-path="如何开始.html"><a href="如何开始.html#r软件包"><i class="fa fa-check"></i><b>2.7</b> R软件包</a>
<ul>
<li class="chapter" data-level="2.7.1" data-path="如何开始.html"><a href="如何开始.html#使用r软件包"><i class="fa fa-check"></i><b>2.7.1</b> 使用R软件包</a></li>
<li class="chapter" data-level="2.7.2" data-path="如何开始.html"><a href="如何开始.html#r软件包内容"><i class="fa fa-check"></i><b>2.7.2</b> R软件包内容</a></li>
<li class="chapter" data-level="2.7.3" data-path="如何开始.html"><a href="如何开始.html#安装r软件包"><i class="fa fa-check"></i><b>2.7.3</b> 安装R软件包</a></li>
</ul></li>
<li class="chapter" data-level="2.8" data-path="如何开始.html"><a href="如何开始.html#安装其他功能"><i class="fa fa-check"></i><b>2.8</b> 安装其他功能</a>
<ul>
<li class="chapter" data-level="2.8.1" data-path="如何开始.html"><a href="如何开始.html#版本控制软件"><i class="fa fa-check"></i><b>2.8.1</b> 版本控制软件</a></li>
<li class="chapter" data-level="2.8.2" data-path="如何开始.html"><a href="如何开始.html#编写r软件包的辅助工具"><i class="fa fa-check"></i><b>2.8.2</b> 编写R软件包的辅助工具</a></li>
</ul></li>
<li class="chapter" data-level="2.9" data-path="如何开始.html"><a href="如何开始.html#获取本书资料"><i class="fa fa-check"></i><b>2.9</b> 获取本书资料</a>
<ul>
<li class="chapter" data-level="2.9.1" data-path="如何开始.html"><a href="如何开始.html#仅查看本书资料"><i class="fa fa-check"></i><b>2.9.1</b> 仅查看本书资料</a></li>
<li class="chapter" data-level="2.9.2" data-path="如何开始.html"><a href="如何开始.html#查看和修改本书资料"><i class="fa fa-check"></i><b>2.9.2</b> 查看和修改本书资料</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="3" data-path="r基础.html"><a href="r基础.html"><i class="fa fa-check"></i><b>3</b> R基础</a>
<ul>
<li class="chapter" data-level="3.1" data-path="r基础.html"><a href="r基础.html#重要的快捷键"><i class="fa fa-check"></i><b>3.1</b> 重要的快捷键</a></li>
<li class="chapter" data-level="3.2" data-path="r基础.html"><a href="r基础.html#基本数据类型"><i class="fa fa-check"></i><b>3.2</b> 基本数据类型</a>
<ul>
<li class="chapter" data-level="3.2.1" data-path="r基础.html"><a href="r基础.html#字符"><i class="fa fa-check"></i><b>3.2.1</b> 字符</a></li>
<li class="chapter" data-level="3.2.2" data-path="r基础.html"><a href="r基础.html#数字"><i class="fa fa-check"></i><b>3.2.2</b> 数字</a></li>
<li class="chapter" data-level="3.2.3" data-path="r基础.html"><a href="r基础.html#逻辑值"><i class="fa fa-check"></i><b>3.2.3</b> 逻辑值</a></li>
</ul></li>
<li class="chapter" data-level="3.3" data-path="r基础.html"><a href="r基础.html#数据运算"><i class="fa fa-check"></i><b>3.3</b> 数据运算</a>
<ul>
<li class="chapter" data-level="3.3.1" data-path="r基础.html"><a href="r基础.html#数字运算"><i class="fa fa-check"></i><b>3.3.1</b> 数字运算</a></li>
<li class="chapter" data-level="3.3.2" data-path="r基础.html"><a href="r基础.html#逻辑和关系运算"><i class="fa fa-check"></i><b>3.3.2</b> 逻辑和关系运算</a></li>
</ul></li>
<li class="chapter" data-level="3.4" data-path="r基础.html"><a href="r基础.html#常见运算函数"><i class="fa fa-check"></i><b>3.4</b> 常见运算函数</a></li>
<li class="chapter" data-level="3.5" data-path="r基础.html"><a href="r基础.html#变量与赋值"><i class="fa fa-check"></i><b>3.5</b> 变量与赋值</a></li>
<li class="chapter" data-level="3.6" data-path="r基础.html"><a href="r基础.html#逻辑控制"><i class="fa fa-check"></i><b>3.6</b> 逻辑控制</a>
<ul>
<li class="chapter" data-level="3.6.1" data-path="r基础.html"><a href="r基础.html#条件"><i class="fa fa-check"></i><b>3.6.1</b> 条件</a></li>
<li class="chapter" data-level="3.6.2" data-path="r基础.html"><a href="r基础.html#循环"><i class="fa fa-check"></i><b>3.6.2</b> 循环</a></li>
</ul></li>
<li class="chapter" data-level="3.7" data-path="r基础.html"><a href="r基础.html#作业"><i class="fa fa-check"></i><b>3.7</b> 作业</a></li>
</ul></li>
<li class="divider"></li>
<li><a href="https://faculty.ecnu.edu.cn/_s31/sgc/main.psp" target="blank">个人主页</a></li>

</ul>

      </nav>
    </div>

    <div class="book-body">
      <div class="body-inner">
        <div class="book-header" role="navigation">
          <h1>
            <i class="fa fa-circle-o-notch fa-spin"></i><a href="./">生态统计学原理</a>
          </h1>
        </div>

        <div class="page-wrapper" tabindex="-1" role="main">
          <div class="page-inner">

            <section class="normal" id="section-">
<div id="r基础" class="section level1 hasAnchor" number="3">
<h1><span class="header-section-number">第 3 章</span> R基础<a href="r基础.html#r基础" class="anchor-section" aria-label="Anchor link to header"></a></h1>
<p>利用R软件做分析的基本方式是在R中编写R代码。那么R代码的编写方式有几种：</p>
<ol style="list-style-type: decimal">
<li><p>双击R程序图标之后，打开Rgui图形界面。然后就可以直接在R console的&gt;后边编写R代码。</p></li>
<li><p>然后单击File-&gt;New script，就可以打开R代码的编辑器，撰写代码。同时，该编辑器的一个功能是可以用Ctrl+R快捷键，把光标所在行的代码发送到R console中。</p></li>
<li><p>用IDE，比如Rstudio。</p></li>
</ol>
<div id="重要的快捷键" class="section level2 hasAnchor" number="3.1">
<h2><span class="header-section-number">3.1</span> 重要的快捷键<a href="r基础.html#重要的快捷键" class="anchor-section" aria-label="Anchor link to header"></a></h2>
<p>上下方向键： 可以调取历史命令</p>
<p>Esc键： 取消当前运算</p>
</div>
<div id="基本数据类型" class="section level2 hasAnchor" number="3.2">
<h2><span class="header-section-number">3.2</span> 基本数据类型<a href="r基础.html#基本数据类型" class="anchor-section" aria-label="Anchor link to header"></a></h2>
<p>R语言中，最常用的三种数据类型是<strong>字符</strong>、<strong>数字</strong>和<strong>逻辑量</strong>，对应的英文分别是<strong>character</strong>, <strong>numeric</strong>和<strong>logical</strong>。下面我们一种种来介绍。</p>
<div id="字符" class="section level3 hasAnchor" number="3.2.1">
<h3><span class="header-section-number">3.2.1</span> 字符<a href="r基础.html#字符" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<p>R里面的字符类型是用两个单引号或双引号包括起来内容。</p>
<div class="sourceCode" id="cb17"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb17-1"><a href="r基础.html#cb17-1" tabindex="-1"></a><span class="st">&#39;4&#39;</span> <span class="co">#字符，character</span></span></code></pre></div>
<pre><code>## [1] &quot;4&quot;</code></pre>
<div class="sourceCode" id="cb19"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb19-1"><a href="r基础.html#cb19-1" tabindex="-1"></a><span class="st">&quot;5&quot;</span></span></code></pre></div>
<pre><code>## [1] &quot;5&quot;</code></pre>
<div class="sourceCode" id="cb21"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb21-1"><a href="r基础.html#cb21-1" tabindex="-1"></a><span class="fu">class</span>(<span class="st">&#39;50&#39;</span>) <span class="co">#class函数可以判断某个值的类型</span></span></code></pre></div>
<pre><code>## [1] &quot;character&quot;</code></pre>
<p>两个引号必须要相互对应。不能出现开始的引号是单引号，结束的引号为双引号。</p>
<div class="sourceCode" id="cb23"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb23-1"><a href="r基础.html#cb23-1" tabindex="-1"></a><span class="st">&#39;hee&quot; #这个是错误的</span></span></code></pre></div>
<p>R其实有一些内置的字符</p>
<div class="sourceCode" id="cb24"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb24-1"><a href="r基础.html#cb24-1" tabindex="-1"></a>letters <span class="co">#英文的字母，小写</span></span></code></pre></div>
<pre><code>##  [1] &quot;a&quot; &quot;b&quot; &quot;c&quot; &quot;d&quot; &quot;e&quot; &quot;f&quot; &quot;g&quot; &quot;h&quot; &quot;i&quot; &quot;j&quot; &quot;k&quot; &quot;l&quot; &quot;m&quot; &quot;n&quot; &quot;o&quot; &quot;p&quot; &quot;q&quot; &quot;r&quot; &quot;s&quot;
## [20] &quot;t&quot; &quot;u&quot; &quot;v&quot; &quot;w&quot; &quot;x&quot; &quot;y&quot; &quot;z&quot;</code></pre>
<div class="sourceCode" id="cb26"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb26-1"><a href="r基础.html#cb26-1" tabindex="-1"></a>LETTERS <span class="co">#大写</span></span></code></pre></div>
<pre><code>##  [1] &quot;A&quot; &quot;B&quot; &quot;C&quot; &quot;D&quot; &quot;E&quot; &quot;F&quot; &quot;G&quot; &quot;H&quot; &quot;I&quot; &quot;J&quot; &quot;K&quot; &quot;L&quot; &quot;M&quot; &quot;N&quot; &quot;O&quot; &quot;P&quot; &quot;Q&quot; &quot;R&quot; &quot;S&quot;
## [20] &quot;T&quot; &quot;U&quot; &quot;V&quot; &quot;W&quot; &quot;X&quot; &quot;Y&quot; &quot;Z&quot;</code></pre>
</div>
<div id="数字" class="section level3 hasAnchor" number="3.2.2">
<h3><span class="header-section-number">3.2.2</span> 数字<a href="r基础.html#数字" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<div class="sourceCode" id="cb28"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb28-1"><a href="r基础.html#cb28-1" tabindex="-1"></a><span class="dv">3</span> <span class="co">#整数</span></span></code></pre></div>
<pre><code>## [1] 3</code></pre>
<div class="sourceCode" id="cb30"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb30-1"><a href="r基础.html#cb30-1" tabindex="-1"></a><span class="fl">3.1415</span> <span class="co">#实数</span></span></code></pre></div>
<pre><code>## [1] 3.1415</code></pre>
<div class="sourceCode" id="cb32"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb32-1"><a href="r基础.html#cb32-1" tabindex="-1"></a><span class="fl">1e+10</span> <span class="co">#科学计数法</span></span></code></pre></div>
<pre><code>## [1] 1e+10</code></pre>
<div class="sourceCode" id="cb34"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb34-1"><a href="r基础.html#cb34-1" tabindex="-1"></a><span class="fl">1e-10</span> </span></code></pre></div>
<pre><code>## [1] 1e-10</code></pre>
<div class="sourceCode" id="cb36"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb36-1"><a href="r基础.html#cb36-1" tabindex="-1"></a><span class="fu">class</span>(<span class="dv">3</span>)</span></code></pre></div>
<pre><code>## [1] &quot;numeric&quot;</code></pre>
<p>R内置常数</p>
<div class="sourceCode" id="cb38"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb38-1"><a href="r基础.html#cb38-1" tabindex="-1"></a>pi <span class="co">#圆周率</span></span></code></pre></div>
<pre><code>## [1] 3.141593</code></pre>
<div class="sourceCode" id="cb40"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb40-1"><a href="r基础.html#cb40-1" tabindex="-1"></a><span class="fu">exp</span>(<span class="dv">1</span>) <span class="co">#自然对数</span></span></code></pre></div>
<pre><code>## [1] 2.718282</code></pre>
<div class="sourceCode" id="cb42"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb42-1"><a href="r基础.html#cb42-1" tabindex="-1"></a><span class="cn">Inf</span> <span class="co">#正无穷</span></span></code></pre></div>
<pre><code>## [1] Inf</code></pre>
<div class="sourceCode" id="cb44"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb44-1"><a href="r基础.html#cb44-1" tabindex="-1"></a><span class="sc">-</span><span class="cn">Inf</span> <span class="co">#负无穷</span></span></code></pre></div>
<pre><code>## [1] -Inf</code></pre>
<div class="sourceCode" id="cb46"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb46-1"><a href="r基础.html#cb46-1" tabindex="-1"></a><span class="cn">NA</span> <span class="co">#not available or missing value</span></span></code></pre></div>
<pre><code>## [1] NA</code></pre>
<div class="sourceCode" id="cb48"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb48-1"><a href="r基础.html#cb48-1" tabindex="-1"></a><span class="cn">NaN</span> <span class="co">#not a number</span></span></code></pre></div>
<pre><code>## [1] NaN</code></pre>
</div>
<div id="逻辑值" class="section level3 hasAnchor" number="3.2.3">
<h3><span class="header-section-number">3.2.3</span> 逻辑值<a href="r基础.html#逻辑值" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<div class="sourceCode" id="cb50"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb50-1"><a href="r基础.html#cb50-1" tabindex="-1"></a><span class="cn">TRUE</span> <span class="co">#True，真</span></span></code></pre></div>
<pre><code>## [1] TRUE</code></pre>
<div class="sourceCode" id="cb52"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb52-1"><a href="r基础.html#cb52-1" tabindex="-1"></a>T</span></code></pre></div>
<pre><code>## [1] TRUE</code></pre>
<div class="sourceCode" id="cb54"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb54-1"><a href="r基础.html#cb54-1" tabindex="-1"></a><span class="cn">FALSE</span> <span class="co">#False, 假</span></span></code></pre></div>
<pre><code>## [1] FALSE</code></pre>
<div class="sourceCode" id="cb56"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb56-1"><a href="r基础.html#cb56-1" tabindex="-1"></a>F</span></code></pre></div>
<pre><code>## [1] FALSE</code></pre>
<div class="sourceCode" id="cb58"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb58-1"><a href="r基础.html#cb58-1" tabindex="-1"></a><span class="fu">class</span>(T)</span></code></pre></div>
<pre><code>## [1] &quot;logical&quot;</code></pre>
</div>
</div>
<div id="数据运算" class="section level2 hasAnchor" number="3.3">
<h2><span class="header-section-number">3.3</span> 数据运算<a href="r基础.html#数据运算" class="anchor-section" aria-label="Anchor link to header"></a></h2>
<div id="数字运算" class="section level3 hasAnchor" number="3.3.1">
<h3><span class="header-section-number">3.3.1</span> 数字运算<a href="r基础.html#数字运算" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<p>针对R中的数值，可以进行基本的算数预算</p>
<div class="sourceCode" id="cb60"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb60-1"><a href="r基础.html#cb60-1" tabindex="-1"></a><span class="dv">1</span><span class="sc">+</span><span class="dv">4</span><span class="sc">+</span><span class="dv">5</span></span></code></pre></div>
<pre><code>## [1] 10</code></pre>
<div class="sourceCode" id="cb62"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb62-1"><a href="r基础.html#cb62-1" tabindex="-1"></a><span class="dv">6</span><span class="sc">+</span><span class="dv">7</span></span></code></pre></div>
<pre><code>## [1] 13</code></pre>
<div class="sourceCode" id="cb64"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb64-1"><a href="r基础.html#cb64-1" tabindex="-1"></a><span class="dv">2-1</span></span></code></pre></div>
<pre><code>## [1] 1</code></pre>
<div class="sourceCode" id="cb66"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb66-1"><a href="r基础.html#cb66-1" tabindex="-1"></a><span class="dv">6</span><span class="sc">+</span><span class="dv">7</span>; <span class="dv">2-1</span></span></code></pre></div>
<pre><code>## [1] 13</code></pre>
<pre><code>## [1] 1</code></pre>
<p>乘除</p>
<div class="sourceCode" id="cb69"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb69-1"><a href="r基础.html#cb69-1" tabindex="-1"></a><span class="dv">4</span><span class="sc">*</span><span class="dv">6</span></span></code></pre></div>
<pre><code>## [1] 24</code></pre>
<div class="sourceCode" id="cb71"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb71-1"><a href="r基础.html#cb71-1" tabindex="-1"></a><span class="dv">1</span><span class="sc">/</span><span class="dv">3</span></span></code></pre></div>
<pre><code>## [1] 0.3333333</code></pre>
<div class="sourceCode" id="cb73"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb73-1"><a href="r基础.html#cb73-1" tabindex="-1"></a><span class="fu">round</span>(<span class="dv">1</span><span class="sc">/</span><span class="dv">3</span>,<span class="dv">3</span>) <span class="co">#round 就是一个R函数</span></span></code></pre></div>
<pre><code>## [1] 0.333</code></pre>
<p>取整取余</p>
<div class="sourceCode" id="cb75"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb75-1"><a href="r基础.html#cb75-1" tabindex="-1"></a><span class="co">#取余数</span></span>
<span id="cb75-2"><a href="r基础.html#cb75-2" tabindex="-1"></a><span class="dv">10</span><span class="sc">%%</span><span class="dv">3</span> </span></code></pre></div>
<pre><code>## [1] 1</code></pre>
<div class="sourceCode" id="cb77"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb77-1"><a href="r基础.html#cb77-1" tabindex="-1"></a><span class="dv">10</span><span class="sc">%/%</span><span class="dv">3</span> <span class="co">#取整</span></span></code></pre></div>
<pre><code>## [1] 3</code></pre>
<div class="sourceCode" id="cb79"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb79-1"><a href="r基础.html#cb79-1" tabindex="-1"></a><span class="dv">2</span><span class="sc">^</span><span class="dv">3</span> <span class="co">#幂运算</span></span></code></pre></div>
<pre><code>## [1] 8</code></pre>
<div class="sourceCode" id="cb81"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb81-1"><a href="r基础.html#cb81-1" tabindex="-1"></a><span class="dv">2</span><span class="sc">^</span>(<span class="dv">1</span><span class="sc">/</span><span class="dv">3</span>)</span></code></pre></div>
<pre><code>## [1] 1.259921</code></pre>
<div id="运算优先级" class="section level4 hasAnchor" number="3.3.1.1">
<h4><span class="header-section-number">3.3.1.1</span> 运算优先级<a href="r基础.html#运算优先级" class="anchor-section" aria-label="Anchor link to header"></a></h4>
<p>R语言中，各种运算其实是有先后顺序，即运算优先级。默认的运算优先级为：^, * /, + -。如果要打破这种优先级，那么就需要用小括号，来告诉R哪几步是一起预算的。</p>
<div class="sourceCode" id="cb83"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb83-1"><a href="r基础.html#cb83-1" tabindex="-1"></a><span class="dv">3</span><span class="sc">+</span><span class="dv">1</span><span class="sc">^</span><span class="dv">2</span><span class="sc">*</span><span class="dv">2</span></span></code></pre></div>
<pre><code>## [1] 5</code></pre>
<div class="sourceCode" id="cb85"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb85-1"><a href="r基础.html#cb85-1" tabindex="-1"></a>(<span class="dv">3</span><span class="sc">+</span><span class="dv">1</span>)<span class="sc">^</span><span class="dv">2</span><span class="sc">*</span><span class="dv">2</span></span></code></pre></div>
<pre><code>## [1] 32</code></pre>
<div class="sourceCode" id="cb87"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb87-1"><a href="r基础.html#cb87-1" tabindex="-1"></a><span class="dv">3</span><span class="sc">+</span><span class="dv">1</span><span class="sc">^</span>(<span class="dv">2</span><span class="sc">*</span><span class="dv">2</span>)</span></code></pre></div>
<pre><code>## [1] 4</code></pre>
</div>
</div>
<div id="逻辑和关系运算" class="section level3 hasAnchor" number="3.3.2">
<h3><span class="header-section-number">3.3.2</span> 逻辑和关系运算<a href="r基础.html#逻辑和关系运算" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<p>除了上述算数运算之外，还可以进行逻辑运算</p>
<div class="sourceCode" id="cb89"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb89-1"><a href="r基础.html#cb89-1" tabindex="-1"></a><span class="cn">TRUE</span> <span class="sc">&amp;</span> <span class="cn">TRUE</span> <span class="co">#与</span></span></code></pre></div>
<pre><code>## [1] TRUE</code></pre>
<div class="sourceCode" id="cb91"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb91-1"><a href="r基础.html#cb91-1" tabindex="-1"></a><span class="cn">TRUE</span> <span class="sc">|</span> <span class="cn">FALSE</span> <span class="co">#或</span></span></code></pre></div>
<pre><code>## [1] TRUE</code></pre>
<div class="sourceCode" id="cb93"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb93-1"><a href="r基础.html#cb93-1" tabindex="-1"></a><span class="sc">!</span><span class="cn">TRUE</span> <span class="co">#反</span></span></code></pre></div>
<pre><code>## [1] FALSE</code></pre>
<p>对数字也可以进行关系预算，比如大小判断</p>
<div class="sourceCode" id="cb95"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb95-1"><a href="r基础.html#cb95-1" tabindex="-1"></a><span class="dv">5</span><span class="sc">&gt;</span><span class="dv">2</span>   <span class="co">#大于</span></span></code></pre></div>
<pre><code>## [1] TRUE</code></pre>
<div class="sourceCode" id="cb97"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb97-1"><a href="r基础.html#cb97-1" tabindex="-1"></a><span class="dv">4</span><span class="sc">&gt;=</span><span class="dv">4</span>  <span class="co">#大于等于</span></span></code></pre></div>
<pre><code>## [1] TRUE</code></pre>
<div class="sourceCode" id="cb99"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb99-1"><a href="r基础.html#cb99-1" tabindex="-1"></a><span class="dv">4</span><span class="sc">==</span><span class="dv">4</span>  <span class="co">#等于</span></span></code></pre></div>
<pre><code>## [1] TRUE</code></pre>
<div class="sourceCode" id="cb101"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb101-1"><a href="r基础.html#cb101-1" tabindex="-1"></a><span class="dv">4</span><span class="sc">&lt;</span><span class="dv">6</span>   <span class="co">#小于</span></span></code></pre></div>
<pre><code>## [1] TRUE</code></pre>
<div class="sourceCode" id="cb103"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb103-1"><a href="r基础.html#cb103-1" tabindex="-1"></a><span class="dv">4</span><span class="sc">&lt;=</span><span class="dv">3</span>  <span class="co">#小于等于</span></span></code></pre></div>
<pre><code>## [1] FALSE</code></pre>
<div class="sourceCode" id="cb105"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb105-1"><a href="r基础.html#cb105-1" tabindex="-1"></a><span class="dv">6</span><span class="sc">!=</span><span class="dv">0</span>  <span class="co">#不等于</span></span></code></pre></div>
<pre><code>## [1] TRUE</code></pre>
</div>
</div>
<div id="常见运算函数" class="section level2 hasAnchor" number="3.4">
<h2><span class="header-section-number">3.4</span> 常见运算函数<a href="r基础.html#常见运算函数" class="anchor-section" aria-label="Anchor link to header"></a></h2>
<p>为了方便运算，R语言中内置了很多常用的运算函数。需要时只要调用相应的函数名称就行，而不需要重复造车。</p>
<p>以下是一些常用函数，如果需要知道具体用法，请用？调出其帮助文档查看。</p>
<div class="sourceCode" id="cb107"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb107-1"><a href="r基础.html#cb107-1" tabindex="-1"></a><span class="fu">exp</span>(<span class="dv">1</span>) <span class="co">#以自然对数为底的幂函数</span></span></code></pre></div>
<pre><code>## [1] 2.718282</code></pre>
<div class="sourceCode" id="cb109"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb109-1"><a href="r基础.html#cb109-1" tabindex="-1"></a><span class="dv">3</span><span class="sc">^</span>(<span class="dv">5</span>) </span></code></pre></div>
<pre><code>## [1] 243</code></pre>
<div class="sourceCode" id="cb111"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb111-1"><a href="r基础.html#cb111-1" tabindex="-1"></a><span class="dv">2</span><span class="sc">^</span>(<span class="dv">1</span><span class="sc">/</span><span class="dv">2</span>) <span class="co">#square root</span></span></code></pre></div>
<pre><code>## [1] 1.414214</code></pre>
<div class="sourceCode" id="cb113"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb113-1"><a href="r基础.html#cb113-1" tabindex="-1"></a><span class="fu">sqrt</span>(<span class="dv">2</span>)</span></code></pre></div>
<pre><code>## [1] 1.414214</code></pre>
<div class="sourceCode" id="cb115"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb115-1"><a href="r基础.html#cb115-1" tabindex="-1"></a><span class="fu">log</span>(<span class="dv">2</span>,<span class="dv">2</span>) <span class="co">#对数函数，第二个参数时base</span></span></code></pre></div>
<pre><code>## [1] 1</code></pre>
<div class="sourceCode" id="cb117"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb117-1"><a href="r基础.html#cb117-1" tabindex="-1"></a><span class="fu">log</span>(<span class="dv">1</span>,<span class="dv">879</span>) </span></code></pre></div>
<pre><code>## [1] 0</code></pre>
<div class="sourceCode" id="cb119"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb119-1"><a href="r基础.html#cb119-1" tabindex="-1"></a><span class="fu">log</span>(<span class="fu">exp</span>(<span class="dv">1</span>))</span></code></pre></div>
<pre><code>## [1] 1</code></pre>
<div class="sourceCode" id="cb121"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb121-1"><a href="r基础.html#cb121-1" tabindex="-1"></a><span class="fu">factorial</span>(<span class="dv">5</span>) <span class="co">#阶乘</span></span></code></pre></div>
<pre><code>## [1] 120</code></pre>
<div class="sourceCode" id="cb123"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb123-1"><a href="r基础.html#cb123-1" tabindex="-1"></a><span class="fu">sin</span>(pi<span class="sc">/</span><span class="dv">6</span>) <span class="co">#三角函数</span></span></code></pre></div>
<pre><code>## [1] 0.5</code></pre>
<div class="sourceCode" id="cb125"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb125-1"><a href="r基础.html#cb125-1" tabindex="-1"></a><span class="fu">tan</span>(pi<span class="sc">/</span><span class="dv">4</span>) </span></code></pre></div>
<pre><code>## [1] 1</code></pre>
<div class="sourceCode" id="cb127"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb127-1"><a href="r基础.html#cb127-1" tabindex="-1"></a><span class="fu">abs</span>(<span class="sc">-</span><span class="dv">5</span>) <span class="co">#绝对值</span></span></code></pre></div>
<pre><code>## [1] 5</code></pre>
<div class="sourceCode" id="cb129"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb129-1"><a href="r基础.html#cb129-1" tabindex="-1"></a><span class="fu">choose</span>(<span class="dv">100</span>,<span class="dv">30</span>) <span class="co">#组合函数</span></span></code></pre></div>
<pre><code>## [1] 2.937234e+25</code></pre>
<div class="sourceCode" id="cb131"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb131-1"><a href="r基础.html#cb131-1" tabindex="-1"></a><span class="fu">runif</span>(<span class="dv">10</span>) <span class="co">#产生随机数的函数</span></span></code></pre></div>
<pre><code>##  [1] 0.10289732 0.70342072 0.52722233 0.18839331 0.05308930 0.98613561
##  [7] 0.38847773 0.10983622 0.29513727 0.02252139</code></pre>
<div class="sourceCode" id="cb133"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb133-1"><a href="r基础.html#cb133-1" tabindex="-1"></a><span class="fu">all.equal</span>(<span class="fl">3.1</span>, <span class="fl">3.2</span>) <span class="co">#比较两个实数</span></span></code></pre></div>
<pre><code>## [1] &quot;Mean relative difference: 0.03225806&quot;</code></pre>
<div class="sourceCode" id="cb135"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb135-1"><a href="r基础.html#cb135-1" tabindex="-1"></a><span class="fu">is.infinite</span>(<span class="dv">10</span>) <span class="co">#判断是否是无穷数</span></span></code></pre></div>
<pre><code>## [1] FALSE</code></pre>
<div class="sourceCode" id="cb137"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb137-1"><a href="r基础.html#cb137-1" tabindex="-1"></a><span class="fu">is.na</span>(<span class="cn">NA</span>) <span class="co">#判断值是否为NA</span></span></code></pre></div>
<pre><code>## [1] TRUE</code></pre>
<p>保留小数位数</p>
<div class="sourceCode" id="cb139"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb139-1"><a href="r基础.html#cb139-1" tabindex="-1"></a><span class="fu">round</span>(pi,<span class="dv">2</span>)</span></code></pre></div>
<pre><code>## [1] 3.14</code></pre>
<div class="sourceCode" id="cb141"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb141-1"><a href="r基础.html#cb141-1" tabindex="-1"></a><span class="fu">ceiling</span>(pi)</span></code></pre></div>
<pre><code>## [1] 4</code></pre>
<div class="sourceCode" id="cb143"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb143-1"><a href="r基础.html#cb143-1" tabindex="-1"></a><span class="fu">floor</span>(pi)</span></code></pre></div>
<pre><code>## [1] 3</code></pre>
</div>
<div id="变量与赋值" class="section level2 hasAnchor" number="3.5">
<h2><span class="header-section-number">3.5</span> 变量与赋值<a href="r基础.html#变量与赋值" class="anchor-section" aria-label="Anchor link to header"></a></h2>
<p>变量是一个值会变，但名字不变的量。例如</p>
<div class="sourceCode" id="cb145"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb145-1"><a href="r基础.html#cb145-1" tabindex="-1"></a>a<span class="ot">=</span><span class="dv">1</span> <span class="co">#a是一个变量，其中‘a’是名字，1是变量值，等号是赋值运算</span></span></code></pre></div>
<p>变量名通常由字母、数字和下划线组成，且不能以数字开头。此外，R语言中的变量名是大小写敏感的，且中间不能包含空格。</p>
<p>当仅输入变量名时，就可以查看变量值。</p>
<div class="sourceCode" id="cb146"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb146-1"><a href="r基础.html#cb146-1" tabindex="-1"></a>a</span></code></pre></div>
<pre><code>## [1] 1</code></pre>
<p>变量的值可以通过赋值运算来改变。正如上面的例子中的等号。但R语言中赋值运算不仅可以用等号，还可以用其他符号。例如</p>
<div class="sourceCode" id="cb148"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb148-1"><a href="r基础.html#cb148-1" tabindex="-1"></a>b<span class="ot">&lt;-</span><span class="dv">4</span> <span class="co">#&lt;-这也是一种赋值预算</span></span>
<span id="cb148-2"><a href="r基础.html#cb148-2" tabindex="-1"></a><span class="dv">5</span><span class="ot">-&gt;</span>c <span class="co">#-&gt;这也是一种赋值运算</span></span></code></pre></div>
<p>由于变量值可变，因此即便变量已经赋值了，还可以再次赋值，这样就会覆盖原来的值。</p>
<div class="sourceCode" id="cb149"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb149-1"><a href="r基础.html#cb149-1" tabindex="-1"></a>a<span class="ot">=</span><span class="dv">10</span></span></code></pre></div>
<p>由于R的数据类型本身有很多中，因此变量包含值，因此其也具有相应的数据类型的变量。</p>
<div class="sourceCode" id="cb150"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb150-1"><a href="r基础.html#cb150-1" tabindex="-1"></a>a<span class="ot">=</span><span class="dv">1</span>; <span class="fu">class</span>(a)</span></code></pre></div>
<pre><code>## [1] &quot;numeric&quot;</code></pre>
<div class="sourceCode" id="cb152"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb152-1"><a href="r基础.html#cb152-1" tabindex="-1"></a>b<span class="ot">=</span><span class="cn">TRUE</span>; <span class="fu">class</span>(b)</span></code></pre></div>
<pre><code>## [1] &quot;logical&quot;</code></pre>
<div class="sourceCode" id="cb154"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb154-1"><a href="r基础.html#cb154-1" tabindex="-1"></a>c<span class="ot">=</span><span class="st">&#39;a&#39;</span>; <span class="fu">class</span>(c)</span></code></pre></div>
<pre><code>## [1] &quot;character&quot;</code></pre>
</div>
<div id="逻辑控制" class="section level2 hasAnchor" number="3.6">
<h2><span class="header-section-number">3.6</span> 逻辑控制<a href="r基础.html#逻辑控制" class="anchor-section" aria-label="Anchor link to header"></a></h2>
<div id="条件" class="section level3 hasAnchor" number="3.6.1">
<h3><span class="header-section-number">3.6.1</span> 条件<a href="r基础.html#条件" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<div class="sourceCode" id="cb156"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb156-1"><a href="r基础.html#cb156-1" tabindex="-1"></a><span class="fu">ifelse</span>(<span class="dv">5</span><span class="sc">&gt;</span><span class="dv">3</span>, <span class="st">&#39;yes&#39;</span>, <span class="st">&#39;no&#39;</span>)</span></code></pre></div>
<pre><code>## [1] &quot;yes&quot;</code></pre>
<div class="sourceCode" id="cb158"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb158-1"><a href="r基础.html#cb158-1" tabindex="-1"></a><span class="cf">if</span>(<span class="dv">4</span><span class="sc">&gt;</span><span class="dv">6</span>)</span>
<span id="cb158-2"><a href="r基础.html#cb158-2" tabindex="-1"></a>  <span class="fu">print</span>(<span class="st">&quot;Y&quot;</span>)</span>
<span id="cb158-3"><a href="r基础.html#cb158-3" tabindex="-1"></a><span class="fu">print</span>(<span class="st">&quot;N&quot;</span>)</span></code></pre></div>
<pre><code>## [1] &quot;N&quot;</code></pre>
<div class="sourceCode" id="cb160"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb160-1"><a href="r基础.html#cb160-1" tabindex="-1"></a><span class="cf">if</span>(<span class="dv">4</span><span class="sc">&gt;</span><span class="dv">6</span>){  <span class="co">#花括号表示当条件为真时的作用域</span></span>
<span id="cb160-2"><a href="r基础.html#cb160-2" tabindex="-1"></a>  <span class="fu">print</span>(<span class="st">&quot;Y&quot;</span>)</span>
<span id="cb160-3"><a href="r基础.html#cb160-3" tabindex="-1"></a>  <span class="fu">print</span>(<span class="st">&quot;N&quot;</span>)</span>
<span id="cb160-4"><a href="r基础.html#cb160-4" tabindex="-1"></a>}</span></code></pre></div>
<div class="sourceCode" id="cb161"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb161-1"><a href="r基础.html#cb161-1" tabindex="-1"></a><span class="cf">if</span>(<span class="dv">5</span><span class="sc">&gt;</span><span class="dv">3</span>){</span>
<span id="cb161-2"><a href="r基础.html#cb161-2" tabindex="-1"></a>  <span class="fu">print</span>(<span class="st">&quot;Yes&quot;</span>)</span>
<span id="cb161-3"><a href="r基础.html#cb161-3" tabindex="-1"></a>  <span class="dv">4</span><span class="sc">+</span><span class="dv">6</span></span>
<span id="cb161-4"><a href="r基础.html#cb161-4" tabindex="-1"></a>}<span class="cf">else</span>{</span>
<span id="cb161-5"><a href="r基础.html#cb161-5" tabindex="-1"></a>  <span class="fu">print</span>(<span class="st">&#39;No&#39;</span>)</span>
<span id="cb161-6"><a href="r基础.html#cb161-6" tabindex="-1"></a>}</span></code></pre></div>
<pre><code>## [1] &quot;Yes&quot;</code></pre>
<pre><code>## [1] 10</code></pre>
</div>
<div id="循环" class="section level3 hasAnchor" number="3.6.2">
<h3><span class="header-section-number">3.6.2</span> 循环<a href="r基础.html#循环" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<div class="sourceCode" id="cb164"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb164-1"><a href="r基础.html#cb164-1" tabindex="-1"></a><span class="cf">for</span>(i <span class="cf">in</span> <span class="dv">1</span><span class="sc">:</span><span class="dv">10</span>) <span class="co">#</span></span>
<span id="cb164-2"><a href="r基础.html#cb164-2" tabindex="-1"></a>  <span class="fu">print</span>(i)</span></code></pre></div>
<pre><code>## [1] 1
## [1] 2
## [1] 3
## [1] 4
## [1] 5
## [1] 6
## [1] 7
## [1] 8
## [1] 9
## [1] 10</code></pre>
<div class="sourceCode" id="cb166"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb166-1"><a href="r基础.html#cb166-1" tabindex="-1"></a><span class="fu">print</span>(i)</span></code></pre></div>
<pre><code>## [1] 10</code></pre>
<div class="sourceCode" id="cb168"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb168-1"><a href="r基础.html#cb168-1" tabindex="-1"></a><span class="cf">for</span>(i <span class="cf">in</span> <span class="dv">1</span><span class="sc">:</span><span class="dv">10</span>){ <span class="co">#</span></span>
<span id="cb168-2"><a href="r基础.html#cb168-2" tabindex="-1"></a>  <span class="fu">print</span>(i)</span>
<span id="cb168-3"><a href="r基础.html#cb168-3" tabindex="-1"></a>  <span class="fu">print</span>(i)</span>
<span id="cb168-4"><a href="r基础.html#cb168-4" tabindex="-1"></a>}</span></code></pre></div>
<pre><code>## [1] 1
## [1] 1
## [1] 2
## [1] 2
## [1] 3
## [1] 3
## [1] 4
## [1] 4
## [1] 5
## [1] 5
## [1] 6
## [1] 6
## [1] 7
## [1] 7
## [1] 8
## [1] 8
## [1] 9
## [1] 9
## [1] 10
## [1] 10</code></pre>
<div class="sourceCode" id="cb170"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb170-1"><a href="r基础.html#cb170-1" tabindex="-1"></a>i<span class="ot">=</span><span class="dv">1</span></span>
<span id="cb170-2"><a href="r基础.html#cb170-2" tabindex="-1"></a><span class="cf">while</span>(i<span class="sc">&lt;</span><span class="dv">10</span>){</span>
<span id="cb170-3"><a href="r基础.html#cb170-3" tabindex="-1"></a>  <span class="fu">print</span>(i)</span>
<span id="cb170-4"><a href="r基础.html#cb170-4" tabindex="-1"></a>  i<span class="ot">=</span>i<span class="sc">+</span><span class="dv">1</span></span>
<span id="cb170-5"><a href="r基础.html#cb170-5" tabindex="-1"></a>}</span></code></pre></div>
<pre><code>## [1] 1
## [1] 2
## [1] 3
## [1] 4
## [1] 5
## [1] 6
## [1] 7
## [1] 8
## [1] 9</code></pre>
</div>
</div>
<div id="作业" class="section level2 hasAnchor" number="3.7">
<h2><span class="header-section-number">3.7</span> 作业<a href="r基础.html#作业" class="anchor-section" aria-label="Anchor link to header"></a></h2>
<ol style="list-style-type: decimal">
<li>请判断下面运算的结果，并在R console中验证你的结果</li>
</ol>
<div class="sourceCode" id="cb172"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb172-1"><a href="r基础.html#cb172-1" tabindex="-1"></a><span class="dv">100</span><span class="sc">+</span><span class="cn">Inf</span> </span>
<span id="cb172-2"><a href="r基础.html#cb172-2" tabindex="-1"></a><span class="dv">1</span><span class="sc">-</span><span class="cn">Inf</span></span>
<span id="cb172-3"><a href="r基础.html#cb172-3" tabindex="-1"></a><span class="dv">1</span><span class="sc">+</span><span class="cn">FALSE</span></span>
<span id="cb172-4"><a href="r基础.html#cb172-4" tabindex="-1"></a><span class="dv">3</span><span class="sc">-</span><span class="cn">TRUE</span></span>
<span id="cb172-5"><a href="r基础.html#cb172-5" tabindex="-1"></a><span class="cn">TRUE</span><span class="sc">+</span><span class="st">&quot;a&quot;</span></span>
<span id="cb172-6"><a href="r基础.html#cb172-6" tabindex="-1"></a><span class="dv">5</span><span class="sc">-</span><span class="st">&quot;1&quot;</span></span>
<span id="cb172-7"><a href="r基础.html#cb172-7" tabindex="-1"></a><span class="cn">TRUE</span> <span class="sc">&amp;</span> <span class="cn">NA</span></span>
<span id="cb172-8"><a href="r基础.html#cb172-8" tabindex="-1"></a>(<span class="dv">2</span><span class="sc">+</span><span class="cn">NA</span>)<span class="sc">/</span><span class="dv">2</span></span>
<span id="cb172-9"><a href="r基础.html#cb172-9" tabindex="-1"></a><span class="fu">exp</span>(<span class="sc">-</span><span class="cn">Inf</span>)</span>
<span id="cb172-10"><a href="r基础.html#cb172-10" tabindex="-1"></a><span class="dv">0</span><span class="sc">^</span><span class="cn">Inf</span></span>
<span id="cb172-11"><a href="r基础.html#cb172-11" tabindex="-1"></a><span class="dv">1</span><span class="sc">^</span><span class="cn">Inf</span></span>
<span id="cb172-12"><a href="r基础.html#cb172-12" tabindex="-1"></a><span class="dv">0</span><span class="sc">/</span><span class="dv">0</span></span>
<span id="cb172-13"><a href="r基础.html#cb172-13" tabindex="-1"></a><span class="cn">Inf</span><span class="sc">-</span><span class="cn">Inf</span></span>
<span id="cb172-14"><a href="r基础.html#cb172-14" tabindex="-1"></a><span class="cn">Inf</span><span class="sc">/</span><span class="cn">Inf</span></span>
<span id="cb172-15"><a href="r基础.html#cb172-15" tabindex="-1"></a>a<span class="ot">=</span><span class="cn">NA</span>; a<span class="sc">==</span><span class="cn">NA</span></span></code></pre></div>
<ol start="2" style="list-style-type: decimal">
<li>请帮忙预测并解释一下下面例子的结果，并说一说它对今后计算的意义</li>
</ol>
<div class="sourceCode" id="cb173"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb173-1"><a href="r基础.html#cb173-1" tabindex="-1"></a><span class="co">#例子1</span></span>
<span id="cb173-2"><a href="r基础.html#cb173-2" tabindex="-1"></a>x <span class="ot">=</span> <span class="fu">sqrt</span>(<span class="dv">2</span>)</span>
<span id="cb173-3"><a href="r基础.html#cb173-3" tabindex="-1"></a>x <span class="sc">*</span> x <span class="sc">==</span> <span class="dv">2</span></span>
<span id="cb173-4"><a href="r基础.html#cb173-4" tabindex="-1"></a></span>
<span id="cb173-5"><a href="r基础.html#cb173-5" tabindex="-1"></a><span class="co">#例子2</span></span>
<span id="cb173-6"><a href="r基础.html#cb173-6" tabindex="-1"></a>x <span class="ot">=</span> <span class="fl">0.3</span> <span class="sc">-</span> <span class="fl">0.2</span></span>
<span id="cb173-7"><a href="r基础.html#cb173-7" tabindex="-1"></a>y <span class="ot">=</span> <span class="fl">0.1</span></span>
<span id="cb173-8"><a href="r基础.html#cb173-8" tabindex="-1"></a>x <span class="sc">==</span> y</span></code></pre></div>
<ol start="3" style="list-style-type: decimal">
<li>帮我构建一个斐波那契序列，并打印出前46个值。其中斐波那契序列的定义如下：</li>
</ol>
<p><span class="math display">\[F_0=0, F_1=1, F_n=F_{n-1}+F_{n-2} (n \ge 2,n \in \mathbf{N})\]</span></p>
<ol start="4" style="list-style-type: decimal">
<li>在上一题的基础上，请给出多少位的时候，斐波那契序列首次出现正无穷。</li>
</ol>

</div>
</div>
            </section>

          </div>
        </div>
      </div>
<a href="如何开始.html" class="navigation navigation-prev navigation-unique" aria-label="Previous page"><i class="fa fa-angle-left"></i></a>

    </div>
  </div>
<script src="libs/gitbook-2.6.7/js/app.min.js"></script>
<script src="libs/gitbook-2.6.7/js/clipboard.min.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-search.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-sharing.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-fontsettings.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-bookdown.js"></script>
<script src="libs/gitbook-2.6.7/js/jquery.highlight.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-clipboard.js"></script>
<script>
gitbook.require(["gitbook"], function(gitbook) {
gitbook.start({
"sharing": {
"github": false,
"facebook": true,
"twitter": true,
"linkedin": false,
"weibo": false,
"instapaper": false,
"vk": false,
"whatsapp": false,
"all": ["facebook", "twitter", "linkedin", "weibo", "instapaper"]
},
"fontsettings": {
"theme": "white",
"family": "sans",
"size": 2
},
"edit": {
"link": "https://github.com/USERNAME/REPO/edit/BRANCH/01-R-basic.Rmd",
"text": "编辑"
},
"history": {
"link": null,
"text": null
},
"view": {
"link": null,
"text": null
},
"download": ["_main.epub"],
"search": {
"engine": "fuse",
"options": null
},
"toc": {
"collapse": "subsection"
}
});
});
</script>

<!-- dynamically load mathjax for compatibility with self-contained -->
<script>
  (function () {
    var script = document.createElement("script");
    script.type = "text/javascript";
    var src = "true";
    if (src === "" || src === "true") src = "https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.9/latest.js?config=TeX-MML-AM_CHTML";
    if (location.protocol !== "file:")
      if (/^https?:/.test(src))
        src = src.replace(/^https?:/, '');
    script.src = src;
    document.getElementsByTagName("head")[0].appendChild(script);
  })();
</script>
</body>

</html>
